package use_case.processRental;

import java.awt.Color;


import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.JScrollBar;
import javax.swing.JLabel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JTextPane;

import use_case.handlereturns.Storage;




public class RentUI implements TotalListener{

	JFrame f, frmMainMenu;
	JList list, shoppingList;
	JLabel lblTotal;
	StyledDocument document;
	Style style;
	JTextPane textPane;
	Ledger kioskLedger;
	
	Customer tom = new Customer("Tom", "1422479246");
	int selectedItem = 0, itemToRemove = 0;
	private Kiosk Redbox;
	DefaultListModel<String> shoppingListModel;
	
	public RentUI(JFrame frmMainMenu, Ledger kioskLedger) {
		this.kioskLedger = kioskLedger;
		this.frmMainMenu = frmMainMenu;
		
		f = new JFrame("Rental Process");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().setBackground(new Color(180, 0, 0));
		f.getContentPane().setLayout(null);
		
		
		Storage storage = new Storage(5);
		storage.initialize();
		//Create Kiosk
		Redbox = new Kiosk(storage, kioskLedger);
		Redbox.rentMode();
		
		//subscribe to listener
		Redbox.getSale().getCheckout().addTotalListener(this);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(217, 90, 160, 125);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		f.getContentPane().add(scrollPane);
		
		list = new JList(storage.getListEntries());
		scrollPane.setViewportView(list);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.addListSelectionListener(new InventoryListListener());
		
		
	
		JScrollPane scrollPaneShopping = new JScrollPane();
		scrollPaneShopping.setBounds(22, 90, 160, 125);
		scrollPaneShopping.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneShopping.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		f.getContentPane().add(scrollPaneShopping);
		
		
		shoppingListModel = new DefaultListModel<String>();
		shoppingList = new JList(shoppingListModel);
		scrollPaneShopping.setViewportView(shoppingList);
		shoppingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		shoppingList.addListSelectionListener(new CartListListener());
		
		
		
		
		JLabel lblRentalProcess = new JLabel("Rental Process");
		lblRentalProcess.setForeground(Color.WHITE);
		lblRentalProcess.setFont(new Font("Tahoma", Font.BOLD, 24));
		lblRentalProcess.setBounds(102, 13, 186, 30);
		f.getContentPane().add(lblRentalProcess);
		
		JLabel lblNewLabel = new JLabel("Inventory");
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblNewLabel.setBounds(239, 56, 108, 30);
		f.getContentPane().add(lblNewLabel);
		
		JButton btnAddItem = new JButton("Add Item");
		btnAddItem.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnAddItem.setBounds(259, 228, 97, 25);
		btnAddItem.addActionListener(new AddItemListener());
		f.getContentPane().add(btnAddItem);
		
		//Remove Item Button
		JButton btnRemoveItem = new JButton("Remove Item");
		btnRemoveItem.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnRemoveItem.setBounds(30, 228, 120, 25);
		btnRemoveItem.addActionListener(new RemoveItemListener());
		f.getContentPane().add(btnRemoveItem);
		
		
		lblTotal = new JLabel("Total:");
		lblTotal.setForeground(Color.WHITE);
		lblTotal.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTotal.setBounds(22, 279, 142, 30);
		f.getContentPane().add(lblTotal);
		
		JButton btnMakePayment = new JButton("Make Payment");
		btnMakePayment.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnMakePayment.setBounds(247, 315, 132, 29);
		btnMakePayment.addActionListener(new PaymentListener());
		f.getContentPane().add(btnMakePayment);
		
		JLabel lblShoppingCart = new JLabel("Shopping Cart");
		lblShoppingCart.setForeground(Color.WHITE);
		lblShoppingCart.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblShoppingCart.setBounds(22, 56, 153, 30);
		f.getContentPane().add(lblShoppingCart);
		
		JButton btnCoupon = new JButton("Discount");
		btnCoupon.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCoupon.setBounds(22, 315, 95, 29);
		btnCoupon.addActionListener(new DiscountListener(this));
		f.getContentPane().add(btnCoupon);
		
		
		f.setSize(417,400);
		f.setVisible(true);

	}
	
	

	//COUPON LISTENER
	//*****************************
	class DiscountListener implements ActionListener{
		private RentUI rentUI;
		
		private DiscountListener(RentUI rentUI){
			this.rentUI = rentUI;	
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			new CouponUI(rentUI);
		}
		
	}
	//******************************
	
	
	class PaymentListener implements ActionListener{
		
		public void actionPerformed(ActionEvent event){
			
			if(Redbox.makePayment(tom)){
				
				//custom title, no icon
				JOptionPane.showMessageDialog(f,
				    "Payment Successful!",
				    "Verification",
				    JOptionPane.PLAIN_MESSAGE);
				f.setVisible(false);
				f.dispose();
				frmMainMenu.setVisible(true);
				
				
			}
			else{
				//custom title, no icon
				JOptionPane.showMessageDialog(f,
				    "Payment Rejected!",
				    "ERROR",
				    JOptionPane.PLAIN_MESSAGE);
			}
		
		}
	}//close inner class
	

	


	class InventoryListListener  implements ListSelectionListener{
		public void valueChanged(ListSelectionEvent event){
			
			if(!event.getValueIsAdjusting()){
				selectedItem = list.getSelectedIndex();
			}
		}
	}//end inner class
	
	
	class CartListListener  implements ListSelectionListener{
		public void valueChanged(ListSelectionEvent event){
			
			if(!event.getValueIsAdjusting()){
				itemToRemove = shoppingList.getSelectedIndex();
			}
		}
	}//end inner class
	
	class RemoveItemListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent event) {
			// TODO Auto-generated method stub
			Redbox.removeItemFromCart(itemToRemove);
			updateShoppingList();
		}
		
	}//end RemoveItemListener
	
	class AddItemListener implements ActionListener{
		
		
		public void actionPerformed(ActionEvent event){
				
			//System.out.println("Item: " + selectedItem + " was selected");
			Redbox.addItemToCart(selectedItem);
			updateShoppingList();				
		
		}
		
	}//close inner class

	public void updateShoppingList(){
		String[] selectionList = Redbox.getSelectedList();
		
		shoppingListModel.clear();
		for (int i = 0; i< selectionList.length;i++)
		{
		shoppingListModel.add(i, selectionList[i]);
		}
	}
	
	
	@Override
	public void onTotalPropertyEvent(String name, double total) {
		// TODO Auto-generated method stub
		 DecimalFormat df = new DecimalFormat("#.##");
	        
		if(name.equals("checkout.total")){
			lblTotal.setText("Total: $" + df.format(total));
		}
		
	}
	
	public void setDiscount(String couponCode){
		
		Redbox.getSale().getCheckout().implementDiscount(couponCode);
	}
}
